import sqlite3

class Database(object):
    """
    Represents the connection to the underlying storage database.
    """
    
    def __init__(self, path = "database.db"):
        self.connection = sqlite3.connect(path, isolation_level=None)
        
    def __del__(self):
        self.connection.commit()
        self.connection.close()

    def execute(self, query, *args):
        """
        Runs a given SQL query.
        """
        cursor = self.connection.cursor()
        cursor.execute(query, args)
        cursor.close()
        
    def getRow(self, query, *args):
        """
        Runs a given SQL select statement and returns the resulting row.
        """
        cursor = self.connection.cursor()
        cursor.execute(query, args)
        row = cursor.fetchone()
        cursor.close()
        return row
    
    def getRows(self, query, *args):
        """
        Runs a given SQL select statement and returns the resulting rows.
        """
        cursor = self.connection.cursor()
        cursor.execute(query, args)
        rows = cursor.fetchall()
        cursor.close()
        return rows
        
    def insertRow(self, query, *args):
        """
        Runs an insert statement and returns the newly generated row identifier.
        """
        cursor = self.connection.cursor()
        cursor.execute(query, args)
        lastrowid = cursor.lastrowid
        cursor.close()
        return lastrowid